package com.example.wxordersystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.wxordersystem.dto.CartDTO;
import com.example.wxordersystem.entity.ProductInfo;
import com.example.wxordersystem.enums.ProductStatusEnum;
import com.example.wxordersystem.enums.ResultEnum;
import com.example.wxordersystem.exception.SellException;
import com.example.wxordersystem.mapper.ProductMapper;
import com.example.wxordersystem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 常红伟
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Override
    public int save(ProductInfo productInfo) {
        return productMapper.insert(productInfo);
    }

    @Override
    public int update(ProductInfo productInfo) {
        return productMapper.updateById(productInfo);
    }

    @Override
    public ProductInfo findOne(String productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public List<ProductInfo> findAll() {
        return productMapper.selectList(null);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        List<ProductInfo> list=productMapper.findByProductStatus(ProductStatusEnum.UP.getCode());

        return list;
    }

    @Override
    public int onSale(String productId) {
        ProductInfo productInfo=productMapper.selectById(productId);

        if (productInfo==null){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }

        if(productInfo.getProductStatusEnum() == ProductStatusEnum.UP){
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }

        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());

        return productMapper.updateById(productInfo);
    }

    @Override
    public int offSale(String productId) {
        ProductInfo productInfo=productMapper.selectById(productId);
        if (productInfo==null){
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }

        if(productInfo.getProductStatusEnum() == ProductStatusEnum.DOWN){
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());

        return  productMapper.updateById(productInfo);

    }


    @Override
    @Transactional(rollbackFor=Exception.class)
    public void increaseStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO:cartDTOList){
            ProductInfo productInfo = new ProductInfo();
            productInfo=productMapper.selectById(cartDTO.getProductId());
            if (productInfo==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result=productInfo.getProductStock()+cartDTO.getProductQuantity();
            productInfo.setProductStock(result);
            productMapper.updateById(productInfo);
        }


    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void decreaseStork(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO:cartDTOList){
            ProductInfo productInfo=productMapper.selectById(cartDTO.getProductId());
           if (productInfo==null){
               throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
           }
           Integer result =productInfo.getProductStock()-cartDTO.getProductQuantity();
           if (result<0){
               throw  new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
           }
           productInfo.setProductStock(result);
           productMapper.updateById(productInfo);
        }

    }
}
